package com.central.apps.util;

import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author GUO
 * @date 2020年12月11日16:15:03
 * HttpClient工具类
 */
@Log4j2
public class HttpUtil {

    /**
     * get请求
     *
     * @return
     */
    public static Object doGet(String url) {
        try {
            HttpClient client = new DefaultHttpClient();
            //发送get请求
            HttpGet request = new HttpGet(url);
            log.info("---------------" + url);
            //设置参数
            HttpResponse response = client.execute(request);

            /**请求发送成功，并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                Object strResult = EntityUtils.toString(response.getEntity());

                return strResult;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Object sendPost(String url, Object paramMap) {

        HttpClient httpClient;
        HttpPost postMethod;
        HttpResponse response;
        Object reponseContent = null;
        try {
            httpClient = HttpClients.createDefault();
            postMethod = new HttpPost(url);
            //postMethod.addHeader("Content-type", "application/json; charset=utf-8");
            //设置请求头
            postMethod.addHeader("accept", "application/json, text/javascript, */*; q=0.01");
            postMethod.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            postMethod.addHeader("connection", "Keep-Alive");
//            postMethod.addHeader("Authorization", "Bearer "+ paramMap.get("token").toString());
//            paramMap.remove("token");
            //设置参数
//            String paramJson = JSONObject.fromObject(paramMap).toString();
            StringEntity stringEntity = new StringEntity(paramMap.toString(), ContentType.create("text/json", "UTF-8"));
            postMethod.setEntity(stringEntity);
            //请求
            response = httpClient.execute(postMethod);
//            log.info(com.alibaba.fastjson.JSONObject.toJSONString(response));
            HttpEntity httpEntity = response.getEntity();
            reponseContent = EntityUtils.toString(httpEntity);
            EntityUtils.consume(httpEntity);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            e.printStackTrace();
        }
        System.out.println(reponseContent.toString());
        return reponseContent;

    }

}